import {
  findMany,
  createInboundLot, findInboundLots, deleteInboundLot,
  findOne,
  inboundUpdateOne,
  inboundFindMany,
  inboundFindOne,
  outbound,
  outboundFindMany,
  desktop,
  exportExcel, findOutboundOrder,
  createOutboundLot, assess,
  deleteOutboundLot,
  findOutboundLots, findShippingFeeMany, updateShippingFee, createShippingFee, deleteShippingFee,
  procureReturnOfGoodsOutbound
} from '../services/store';
import {createPersonnel, findDicMany} from "../services/personnel";


export default {
  namespace: 'store',
  state: {},

  effects: {
    * findMany({payload}, {call, put}) {
      const response = yield call(findMany, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {stores: result}
        });

        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    /**
     * 查找原料的库存
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, void, *>}
     */
    * findOne({payload}, {call, put}) {
      const response = yield call(findOne, payload);
      if (response) {
        const {result, status, message} = response;

        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            stores: result
          }
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    // * statistics({payload}, {call, put}) {
    //   const response = yield call(statistics, payload);
    //
    //   if (response) {
    //     const {result, status, message} = response;
    //     if (status !== "ok") {
    //       yield put({
    //         type: 'onNotify',
    //         payload: {message, type: 'error'}
    //       });
    //       return;
    //     }
    //     yield put({
    //       type: 'onDataLoad',
    //       payload: {
    //         statistics: result
    //       }
    //     });
    //     return;
    //   }
    //
    //   yield put({
    //     type: 'onNotify',
    //     payload: {message: "无服务！", type: 'error'}
    //   });
    // },

    * assess({payload}, {call, put}) {
      console.log('paaaaa', payload);
      const response = yield call(assess, payload);
      console.log('assess response', response);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result = {}, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      console.log('assess res', result);
      yield put({type: 'onDataLoad', payload: {assess: result}});
      return response;
    },
    * desktop({payload}, {call, put}) {
      const response = yield call(desktop, payload);
      if (response) {
        const {result, status, message} = response;

        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            data: result
          }
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * createInboundLot({payload}, {call, put}) {
      payload.lotCode = 'INT' + new Date().valueOf();
      const response = yield call(createInboundLot, payload);
      return response;
    },

    * findInboundLots({payload}, {call, put}) {
      const response = yield call(findInboundLots, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {status, result, message} = response;
      if (status !== "ok" || !result) {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({
        type: 'onDataLoad', payload: {inboundLots: result}
      });
      return response;

    },
    * deleteInboundLot({payload}, {call, put}) {

      const response = yield call(deleteInboundLot, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findInboundOrder({payload}, {call, put}) {
      const response = yield call(inboundFindOne, payload);
      return response;
    },
    * inboundFindMany({payload}, {call, put}) {
      const response = yield call(inboundFindMany, payload);
      if (response) {
        const {result, status, message} = response;

        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            orders: result
          }
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * InboundUpdateOne({payload}, {call, put}) {
      const response = yield call(inboundUpdateOne, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result
          }
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * outbound({payload}, {call, put}) {
      const response = yield call(outbound, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result
          }
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * outboundFindMany({payload}, {call, put}) {
      const response = yield call(outboundFindMany, payload);
      if (response) {
        const {result, status, message} = response;
        console.log('inboundFindMany resule', result);
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            orders: result
          }
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * findOutboundOrder({payload}, {call, put}) {

      const response = yield call(findOutboundOrder, payload);
      return response;
    },

    * exportExcel({payload}, {call, put}) {
      const response = yield call(exportExcel, payload);
      if (response) {
        const {status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: message
          });
          return;
        }
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * createOutboundLot({payload}, {call, put}) {
      payload.lotCode = 'OUT' + new Date().valueOf();
      console.log('create out lot', payload);
      const response = yield call(createOutboundLot, payload);
      return response;
    },

    * deleteOutboundLot({payload}, {call, put}) {
      const response = yield call(deleteOutboundLot, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {status, result, message} = response;
      yield put({
        type: 'onDataLoad', payload: {result}
      });
      const _payload = status !== "ok" ? message : {message: "操作成功！", type: 'info'};
      yield put({type: 'onNotify', payload: _payload});
    },

    * findOutboundLots({payload}, {call, put}) {
      const response = yield call(findOutboundLots, payload);
      return response;
    },
    * createShippingFee({payload}, {call, put}) {
      const response = yield call(createShippingFee, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * findShippingFeeMany({payload}, {call, put}) {
      const response = yield call(findShippingFeeMany, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * updateShippingFee({payload}, {call, put}) {
      const response = yield call(updateShippingFee, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * deleteShippingFee({payload}, {call, put}) {
      const response = yield call(deleteShippingFee, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    /**
     * 采购退货出库
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    * procureReturnOfGoodsOutbound({payload}, {call, put}) {
      const response = yield call(procureReturnOfGoodsOutbound, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {personnelData: result}});
      return response;
    }
  },

  reducers: {
    onDataLoad(state, {payload}) {
      state._notify = undefined;
      return {...state, ...payload};
    },
    onNotify(state, {payload}) {
      state._notify = payload;
      return {...state, ...payload};
    }
  }
};
